package org.example.springbootquickstart.service;

import org.example.springbootquickstart.pojo.User;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UsersService {

    public static List<User> users=new ArrayList<>();

    static {
        try(
//       BufferedReader bufferedReader=new BufferedReader(new FileReader("C:\\Users\\464508405\\IdeaProjects\\springbootdemo1\\springbootquickstart\\src\\main\\resources\\user.txt"));
          //springboot启动
//          BufferedReader bufferedReader=new BufferedReader(new FileReader("springbootquickstart/src/main/resources/user.txt"));
          //test类启动
//          BufferedReader bufferedReader=new BufferedReader(new FileReader("src/main/resources/user.txt"));

          //类加载ClassLoader.getResourceAsStream("user.txt")
          BufferedReader bufferedReader = new BufferedReader(
                  new InputStreamReader(
                          Objects.requireNonNull(UsersService.class.getClassLoader().getResourceAsStream("user.txt")))

          )
        )
        {
            String line;
            while ((line = bufferedReader.readLine())!=null)
            {
                String[] strings=line.split(",");
                users.add(new User(Integer.parseInt(strings[0]),strings[1],strings[2],strings[3],Integer.parseInt(strings[4]),strings[5]));

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public List<User> getUsers()
    {
        return users;
    }



//    public void addUser()
//    {
//        String currentDir = System.getProperty("user.dir");
//        System.out.println("当前工作目录: " + currentDir);
//    }
}
